package org.telegram.bot.service.impl;

import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotMessageTextCheck;
import org.telegram.bot.service.IBotGroupService;
import org.telegram.bot.service.IMessageService;
import org.telegram.telegrambots.meta.api.objects.Update;

/**
 * @Author: wyx
 * @Date: 2023/5/27 21:43
 */
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private IBotGroupService groupService;
    
    @Override
    public void handle(Update update, Long chatId,String botId) {
        //违禁词检测:有些群信息改变记录也有走这里(群名称、类型修改等等)，但是没有text，无需操作检测
        if(update.hasMessage()) {
            String text = "";
            if(StringUtils.isNotEmpty(update.getMessage().getText())){
                text = update.getMessage().getText();
            }
            if(StringUtils.isNotEmpty(update.getMessage().getCaption())){
                text+=update.getMessage().getCaption();
            }
            String nickName = "";
            if(StringUtils.isNotEmpty(update.getMessage().getFrom().getFirstName())){
                nickName += update.getMessage().getFrom().getFirstName();
            }
            if(StringUtils.isNotEmpty(update.getMessage().getFrom().getLastName())){
                nickName += update.getMessage().getFrom().getLastName();
            }
            
            BotMessageTextCheck textCheck = new BotMessageTextCheck()
                    .setChatId(chatId)
                    .setChatType(update.getMessage().getChat().getType())
                    .setBotId(botId)
                    .setUserId(update.getMessage().getFrom().getId())
                    .setMessageId(update.getMessage().getMessageId())
                    .setNickName(nickName)
                    .setText(text)
                    .setEntities(update.getMessage().getEntities());
            //预览的链接
            if (update.getMessage().getLinkPreviewOptions()!=null){
                textCheck.setLinkPreview(update.getMessage().getLinkPreviewOptions().getUrlField());
            }
            groupService.messageTextCheck(textCheck);
        }
    }

}
